package org.example;

import javafx.collections.transformation.SortedList;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisDemoGeneric {
    public static void test1(Jedis jedis) {
        jedis.flushAll();  //首先清空数据库，防止每次调用方法的时候，里面的数据造成影响
//        String pong = jedis.ping();
//        System.out.println(pong);
//
//        jedis.set("key","111");
//        String value = jedis.get("key");
//        System.out.println(value);

//        SetParams setParams = new SetParams();
//        setParams.ex(30);
//        setParams.nx();
//        jedis.set("key1","111",setParams);
//        long time = jedis.ttl("key1");
//        System.out.println(time);

//        jedis.set("key","111");
//        boolean result = jedis.exists("key");
//        System.out.println("result: " + result);

//        jedis.set("key","111");
//        jedis.set("key2","222");
//        long del = jedis.del("key");
//        System.out.println("del: " +del);
//        boolean v1 = jedis.exists("key");
//        boolean v2 = jedis.exists("key2");
//        System.out.println("v1: " + v1);
//        System.out.println("v2: " + v2);

//        jedis.set("key1","111");
//        jedis.set("key2","222");
//        jedis.set("key3","333");
//        Set<String> keys = jedis.keys("*");
//        System.out.println("keys: " + keys);

        jedis.set("key1","111");
        String type = jedis.type("key1");
        System.out.println("type: " + type);
        jedis.lpush("key2","111","222");
        type = jedis.type("key2");
        System.out.println("type: " + type);
        jedis.hset("key3","f1","111");
        type = jedis.type("key3");
        System.out.println("type: " + type);
        jedis.sadd("key4","111","222");
        type = jedis.type("key4");
        System.out.println("type: " + type);
        jedis.zadd("key5",90.5,"111");
        type = jedis.type("key5");
        System.out.println("type: " + type);
    }

    public static void test2(Jedis jedis) {
        jedis.flushAll();

//        jedis.mset("key1","111","key2","222","key3","333");
//        List<String> list = jedis.mget("key1","key2","key3");
//        System.out.println("list: " + list);

//        jedis.set("key1","abcdefg");
//        String v = jedis.get("key1");
//        System.out.println("v: " + v);
//        jedis.setrange("key1",2,"xyz");
//        v = jedis.getrange("key1",0,-1);
//        System.out.println("v: " + v);

//        jedis.set("key1","abcdefg");
//        String v = jedis.get("key1");
//        System.out.println("v: " + v);
//        jedis.append("key1","xyz");
//        v = jedis.get("key1");
//        System.out.println("v: " + v);

        jedis.set("key1","10");
        String v = jedis.get("key1");
        System.out.println("v: " + v);
        long result = jedis.incr("key1");
        System.out.println("result: " + result);
        result = jedis.decr("key1");
        System.out.println("result: " + result);
        result = jedis.incrBy("key1",10);
        System.out.println("result: " + result);
        result = jedis.decrBy("key1",20);
        System.out.println("result: " + result);
        double d = jedis.incrByFloat("key1",13.14);
        System.out.println("d: " + d);
    }

    public static void test3(Jedis jedis) {
        jedis.flushAll();

//        jedis.lpush("key1","111","222","333");
//        List<String> list = jedis.lrange("key1",0,-1);
//        System.out.println("list: " + list);

//        jedis.rpush("key1","111","222","333");
//        List<String> list = jedis.lrange("key1",0,-1);
//        System.out.println("list: " + list);
//        String pop = jedis.lpop("key1");
//        System.out.println("lpop: " + pop);
//        pop = jedis.rpop("key1");
//        System.out.println("rpop: " + pop);

        List<String> list = jedis.lrange("key1",0,-1);
        System.out.println("list: " + list);
        list = jedis.blpop(300,"key1","key2");
        System.out.println("list: " + list);
    }

    private static void test4(Jedis jedis) {
        jedis.flushAll();

//        //hset可以一次只设置一对file-value
//        jedis.hset("key1","f1","111");
//        String v = jedis.hget("key1","f1");
//        System.out.println("v: " + v);
//        //hset也可通过map来一次设置多对file-set
//        Map<String,String> map = new HashMap<>();
//        map.put("f1","111");
//        map.put("f2","222");
//        jedis.hset("key2",map);
//        v = jedis.hget("key2","f2");
//        System.out.println("v: " + v);

//        jedis.hset("key1","f1","111");
//        boolean b = jedis.hexists("key1","f1");
//        System.out.println("b: " + b);
//        b = jedis.hexists("key1","key2");
//        System.out.println("b: " + b);

//        jedis.hset("key1","f1","111");
//        jedis.hdel("key1","f1");
//        String s = jedis.hget("key1","f1");
//        System.out.println("s: " + s);

//        Map<String,String> map = new HashMap<>();
//        map.put("f1","111");
//        map.put("f2","222");
//        map.put("f3","333");
//        map.put("f4","444");
//        jedis.hset("key1",map);
//        Set<String> set = jedis.hkeys("key1");
//        System.out.println("keys: " + set);
//        List<String> list = jedis.hvals("key1");
//        System.out.println("values: " + list);

        Map<String,String> map = new HashMap<>();
        map.put("f1","111");
        map.put("f2","222");
        map.put("f3","333");
        map.put("f4","444");
        jedis.hmset("key1",map);
        List<String> list = jedis.hmget("key1","f1","f2","f4");
        System.out.println("list: " + list);
    }

    public static void test5(Jedis jedis) {
        jedis.flushAll();

//        jedis.sadd("key1","111","222","444","333");
//        Set<String> set = jedis.smembers("key1");
//        boolean b = jedis.sismember("key1","111");
//        System.out.println("b: " + b);
//        b = jedis.sismember("key1","10101010");
//        System.out.println("b: " + b);
//        System.out.println("members: " + set);

//        jedis.sadd("key1","111","222","444","333");
//        long len = jedis.scard("key1");
//        System.out.println("len: " + len);

//        jedis.sadd("key1","111","222","444","333");
//        String pop = jedis.spop("key1");
//        System.out.println("pop: " + pop);
//        pop = jedis.spop("key1");
//        System.out.println("pop: " + pop);
//        pop = jedis.spop("key1");
//        System.out.println("pop: " + pop);
//        pop = jedis.spop("key1");
//        System.out.println("pop: " + pop);
//        pop = jedis.spop("key1");
//        System.out.println("pop: " + pop);

        jedis.sadd("key1","111","222","444","333");
        jedis.sadd("key2","111","222","777","888");
        long len = jedis.sinterstore("key3","key1","key2");
        System.out.println("len: " + len);
        Set<String> set = jedis.smembers("key3");
        System.out.println("set: " + set);
//        Set<String> set = jedis.sinter("key1","key2");
//        System.out.println("inter: " + set);
    }

    public static void test6(Jedis jedis) {
        jedis.flushAll();

//        jedis.zadd("key1",93.5,"zhangsan");
//        List<String> list = jedis.zrange("key1",0,-1);
//        System.out.println("set: " + list);
//        List<Tuple> tuple = jedis.zrangeWithScores("key1",0,-1);
//        System.out.println("tuple: " + tuple);

//        Map<String,Double> map = new HashMap<>();
//        map.put("zhangsan",93.0);
//        map.put("lisi",95.5);
//        map.put("wangwu",100.0);
//        jedis.zadd("key1",map);
//        List<String> list = jedis.zrange("key1",0,-1);
//        System.out.println("list: " + list);
//        List<Tuple> tuple = jedis.zrangeWithScores("key1",0,-1);
//        System.out.println("tuple: " + tuple);

//        Map<String,Double> map = new HashMap<>();
//        map.put("zhangsan",93.0);
//        map.put("lisi",95.5);
//        map.put("wangwu",100.0);
//        jedis.zadd("key1",map);
//        long len = jedis.zcard("key1");
//        System.out.println("len: " + len);

//        Map<String,Double> map = new HashMap<>();
//        map.put("zhangsan",93.0);
//        map.put("lisi",95.5);
//        map.put("wangwu",100.0);
//        jedis.zadd("key1",map);
//        long count = jedis.zrem("key1","zhangsan","lisi");
//        System.out.println("count: " + count);
//        List<Tuple> tuple = jedis.zrangeWithScores("key1",0,-1);
//        System.out.println("tuple: " + tuple);

//        Map<String,Double> map = new HashMap<>();
//        map.put("zhangsan",93.0);
//        map.put("lisi",95.5);
//        map.put("wangwu",100.0);
//        jedis.zadd("key1",map);
//        Double score = jedis.zscore("key1","zhangsan"); //这里尽量使用包装类，因为如果要获取的分数的元素不在zset中就会返回null，而普通类型double无法接收null
//        System.out.println("score: " + score);
//        score = jedis.zscore("key1","zhaoliu");
//        System.out.println("score: " + score);

        Map<String,Double> map = new HashMap<>();
        map.put("zhangsan",93.0);
        map.put("lisi",95.5);
        map.put("wangwu",100.0);
        jedis.zadd("key1",map);
        Long rank = jedis.zrank("key1","zhangsan");
        System.out.println("rank: " + rank);
        rank = jedis.zrank("key1","lisi");
        System.out.println("rank: " + rank);
        rank = jedis.zrank("key1","wangwu");
        System.out.println("rank: " + rank);
        rank = jedis.zrank("key1","zhaoliu");
        System.out.println("rank: " + rank);
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
//            test5(jedis);
            test6(jedis);
        }
    }
}
